package com.component.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.component.domain.MDicti;
import com.component.service.MDictiService;
import com.component.mapper.MDictiMapper;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class MDictiServiceImpl extends ServiceImpl<MDictiMapper, MDicti>
    implements MDictiService{

    @Override
    public IPage<MDicti> parentPage(Integer pageNumber, Integer pageSize) {
        LambdaQueryWrapper<MDicti> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MDicti::getLevel,0);
        lambdaQueryWrapper.eq(MDicti::getPid,0);
        IPage<MDicti> mDictiIPage=this.baseMapper.selectPage(new Page(pageNumber,pageSize),lambdaQueryWrapper);
        return mDictiIPage;
    }

    @Override
    public IPage<MDicti> page(String pid, Integer pageNumber, Integer pageSize) {
        LambdaQueryWrapper<MDicti> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MDicti::getLevel,1);
        lambdaQueryWrapper.eq(MDicti::getPid,pid);
        IPage<MDicti> mDictiIPage=this.baseMapper.selectPage(new Page(pageNumber,pageSize),lambdaQueryWrapper);
        return mDictiIPage;
    }

    @Override
    public MDicti getByKey(String key) {
        LambdaQueryWrapper<MDicti> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MDicti::getDictKey,key);
        lambdaQueryWrapper.eq(MDicti::getLevel,0);
        MDicti mDicti=this.baseMapper.selectOne(lambdaQueryWrapper);
        return mDicti;
    }

    @Override
    public MDicti getByLabelKey(String pid, String labelkey) {
        LambdaQueryWrapper<MDicti> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MDicti::getPid,pid);
        lambdaQueryWrapper.eq(MDicti::getLevel,1);
        lambdaQueryWrapper.eq(MDicti::getLabelKey,labelkey);
        MDicti mDicti=this.baseMapper.selectOne(lambdaQueryWrapper);
        return mDicti;
    }

    @Override
    public boolean existsList(String pid) {
        LambdaQueryWrapper<MDicti> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MDicti::getPid,pid);
        lambdaQueryWrapper.eq(MDicti::getLevel,1);
        Integer total=this.baseMapper.selectCount(lambdaQueryWrapper);
        return total > 0;
    }

    @Override
    public MDicti getMDicti(String parentKey, String labelKey) {
        MDicti mDicti=getByKey(parentKey);
        if(mDicti==null){
            return null;
        }
        return getByLabelKey(mDicti.getId().toString(),labelKey);
    }

    @Override
    public List<MDicti> getMDictis(String parentKey) {
        MDicti mDicti = getByKey(parentKey);
        if(mDicti == null){
            return null;
        }
        LambdaQueryWrapper<MDicti> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(MDicti::getPid,mDicti.getId().toString());
        List<MDicti> mDictiList=this.baseMapper.selectList(queryWrapper);
        return mDictiList;
    }

    @Override
    public List<Map<String, Object>> getLabelDicts(String parentKey) {
        List<MDicti> list=getMDictis(parentKey);
        if(list==null || list.size() == 0){
            return Collections.emptyList();
        }
        return list.stream().map((e)->{
            Map<String, Object> map = new HashMap<>();
            map.put("key",e.getLabelKey());
            map.put("value",e.getLabelVal());
            return map;
        }).collect(Collectors.toList());

    }


}




